/*
This is a sample: How to use small graint size (16K) in kv_put :
1. get disk location: disk_location=kv_put_index(key, key_size, value_size);  ( must known value_size first, and value_size is in B, NOT 16k )
2. give <start disk_location, char* value, value_size_in_16k>   (value_size is in "16k" now!
   next_start_disk_location = kv_put_seg (pre_start_disk_location, value_buffer, value_size_in_16k);
   in success return: keep the invariant: next_start_disk_location=pre_start_disk_location+value_size_16k
XXX: 
1. kv_get is often random read, so small graint size may be unuseful, kv_put is often sequencial write, so using small graint size.
  ( in condition of "kernel thread pdflush", "kernel pdflush buffer" NOT considered )
2. kv_put_index(), kv_put_seg() prototype in <kvs.h>
3. FIXME: WITHOUT stable test.
*/

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include <kvs.h>

static void _fill(char *value, int size);

int main()
{
	char *key, *value, *buffer, *ptr;
	int i, disk_location;
	KVS_SET kvs;

	kvs.init_type=KVS_CREATE;  
	kvs.size_in_g=1000;
	strcpy(kvs.disk_file, "/tmp/disk");
	strcpy(kvs.disk_log, "/tmp/kvs_disk.log");
	strcpy(kvs.sync_log, "/tmp/kvs_sync.log");
	strcpy(kvs.index_log, "/tmp/kvs_index.log");
	kv_init(&kvs, NULL);

	key=malloc(128);
	value=malloc(16*1024*10+1);
	buffer=malloc(16*1024*10+1);
	memset(key, 0, 128);
	memset(value, 0, 16*1024*10+1);
	memset(buffer, 0, 16*1024*10+1);

	strcpy(key, "this is a key");
	_fill(value, 16*1024*10);

	disk_location=kv_put_index(key, strlen(key), 16*1024*10);
	ptr=value;
	for (i=0; i<10; i++)
	{
		disk_location=kv_put_seg(disk_location, ptr, 1);   /* new disk_location */
		ptr+=16*1024;
	}

	kv_get(key, strlen(key), buffer, 16*1024*10+1);
	//printf ("%s\n",value);
	printf ("compare different:%d\n",strcmp(buffer, value));

	kv_exit();

	free(key);
	free(value);
	free(buffer);
	return 0;
}

static void _fill(char *value, int size)
{
	int i;
	for (i=0; i<size; i++)
	{
		value[i]='a';
	}
}
